package com.systemframework.sms;


import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.systemframework.constants.Constants;
import com.systemframework.exception.ResultException;
import com.systemframework.properties.SmsProperties;
import com.systemframework.redis.RedisCache;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.concurrent.TimeUnit;


/**
 * @author summer
 * @date 2023-02-16 11:27
 */
@Configuration
@Slf4j
@EnableConfigurationProperties(SmsProperties.class)
@ConditionalOnClass({SmsProperties.class})
public class AliSms {


    private SmsProperties smsProperties;

    private RedisCache redisCache;

    @Autowired
    public void setRedisCache(RedisCache redisCache) {
        this.redisCache = redisCache;
    }

    @Autowired
    public void setSmsProperties(SmsProperties smsProperties) {
        this.smsProperties = smsProperties;
    }

    private static final String ALY_SMS_DOMAIN="dysmsapi.aliyuncs.com";

    private static final String ALY_SMS_ACTION="SendSms";

    private static final String ALY_SMS_VERSION="2017-05-25";


    public void sendSms(String phone){

        if(StrUtil.isEmpty(phone)){
            throw new ResultException("手机号码不能为空");
        }
        String randomNumbers = redisCache.get(Constants.SMS_ + phone);
        if (StrUtil.isEmpty(randomNumbers)) {
             randomNumbers = RandomUtil.randomNumbers(smsProperties.getRandomLength());
        }
         DefaultProfile profile = DefaultProfile.getProfile(smsProperties.getRegionId(), smsProperties.getAccessKeyId(), smsProperties.getSecret());
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain(smsProperties.getSmsDomain());
        request.setVersion(smsProperties.getSmsVersion());
        request.setAction(smsProperties.getSmsAction());
        HashMap<String, Object> param = MapUtil.newHashMap(4);
        param.put("code",randomNumbers);
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", smsProperties.getSignName());
        request.putQueryParameter("TemplateCode", smsProperties.getTemplateCode());
        request.putQueryParameter("TemplateParam", JSON.toJSONString(param));
        try {
            CommonResponse response = client.getCommonResponse(request);
            boolean success = response.getHttpResponse().isSuccess();
            log.info("sms:{}",success);
            redisCache.set(Constants.SMS_+phone,randomNumbers,5, TimeUnit.MINUTES);
        }catch (Exception e){
            log.error("sms:{}",e.getMessage(),e);
            throw new ResultException(e.getMessage());
        }
    }



}
